<?php
/**
 * Created by PhpStorm.
 * User: sykbxc
 * Date: 2019/4/11
 * Time: 10:33
 */

namespace App\Services\AdminService\Services;


use App\Enum\ZeroOneEnum;
use App\Services\AdminService\Models\AdminPermissionsModel;
use App\Services\AdminService\Models\AdminRecordModel;
use App\Services\BaseService;
use Illuminate\Http\Request;

class RecordService extends BaseService
{

    /**
     * 查询管理员名称
     * @param $query
     * @param $param
     * @param string $func
     * @return mixed
     * @throws \Exception
     */
    public static function getData($query, $param, $func = '')
    {
        return parent::getData($query, $param, function ($query){
                    return $query->with('admin');
        });
    }

    /**
     * 插入日志
     * @param Request $request
     * @param $response
     * @param $admin_id
     * @return AdminRecordModel|bool
     */
    public static function create(Request $request, $response, $admin_id)
    {
        $permission = AdminPermissionsModel::where("name", $request->route()->getName())->first();
        if ( empty($permission) ||$permission['is_page'] == ZeroOneEnum::ONE || (strpos($request->path(),'data')!==false) ) {
            return false;
        }
        $model = new AdminRecordModel();
        $model->admin_id = $admin_id;
        $model->req_url = $request->path();
        $model->req_name = $request->route()->getName();
        $model->req_display_name = $permission['display_name'];
        $model->req_param = json_encode($request->all());
        $model->rep_param = $response->content();
        return $model->save() ? $model : false;
    }



}